#include "custombutton.h"
#include <QPainter>

CustomButton::CustomButton(QWidget *parent) :QPushButton(parent){}

CustomButton::CustomButton(const QString &iconPath, int x, int y, QWidget *parent)
    : QPushButton(parent) {
    setIcon(QIcon(iconPath));
    setFixedSize(45, 45);
    move(x, y);
}

void CustomButton::paintEvent(QPaintEvent *event) /*override */{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制透明背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::transparent);
    painter.drawRect(rect());

    // 绘制按钮图片
    QPixmap pixmap = icon().pixmap(rect().size());
    painter.drawPixmap(rect(), pixmap, pixmap.rect());

    // 绘制按钮文本
    painter.setPen(Qt::white);
    painter.drawText(rect(), Qt::AlignCenter, text());
}
